from kafka import KafkaProducer
import json
import datetime
import random


def KafkaProduce(topic,data):
    # #测试环境kafka地址
    # producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
    #                          bootstrap_servers=['10.10.15.218:9092'])
    # #演示环境kafka地址
    # producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
    #                          bootstrap_servers=['10.10.102.163:9092'])
    #泰安环境地址
    producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
                             bootstrap_servers=['10.10.30.249:9092'])

    # time = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
    # data = {"deviceId": "10100521",
    #         "timestamp": time,
    #         "values": {"No1TankLevel": json.dumps(round(random.uniform(1, 5), 2)),
    #                    "No2TankLevel": json.dumps(round(random.uniform(0, 1.5), 2))}
    #         }
    producer.send(topic, data)
    producer.close()

# if __name__ == '__main__':
#     KafkaProduce()
